package com.example.maggie.meizinote.mvp.ui.widget;

import android.content.Context;
import android.support.v4.view.ActionProvider;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.maggie.meizinote.R;
import com.example.maggie.meizinote.mvp.ui.adapter.HomeMenuAdapter;

/**
 * Created by addis on 2018/4/21.
 */
public class HomeActionProvider extends ActionProvider {
    LinearLayout customMenu;
    private Context mContext;
    HomeMenuAdapter adapter;

    BaseQuickAdapter.OnItemClickListener clickListener;
    BaseQuickAdapter.OnItemLongClickListener longClickListener;

    /**
     * Creates a new instance.
     *
     * @param context Context for accessing resources.
     */
    public HomeActionProvider(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public View onCreateActionView() {
        customMenu = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.item_home_menu, null);
        RecyclerView menu_recycler_view = customMenu.findViewById(R.id.menu_recycler_view);
        if (adapter == null) adapter = new HomeMenuAdapter();
        menu_recycler_view.setLayoutManager(new LinearLayoutManager(mContext));// 布局管理器
        menu_recycler_view.setAdapter(adapter);
        menu_recycler_view.setItemAnimator(new DefaultItemAnimator());
        if (clickListener != null)
            adapter.setOnItemClickListener(clickListener);
        if (longClickListener != null)
            adapter.setOnItemLongClickListener(longClickListener);
        //return null 时才会调用onPrepareSubMenu方法
        return customMenu;
    }

    public void setClickListener(BaseQuickAdapter.OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(BaseQuickAdapter.OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    @Override
    public boolean hasSubMenu() {
        return true;
    }
}
